package com.xms.form.core.component;

import com.alibaba.fastjson.annotation.JSONField;
import com.xms.schema.entity.Attribute;
import com.xms.utils.JsonUtil;

import java.io.Serializable;

/**
 * <p>
 * ControlDescriptor
 * </p>
 *
 * @author migo
 * @since 2021/9/3
 */
public class ControlDescriptor implements Serializable {
    public String Name;
    public String EntityName;
    private FormControlType _controlType;

    @JSONField(name="ControlType")
    public FormControlType getControlType(){return _controlType;}

    public void setControlType(Object type){
        if(type == null) {
            _controlType = FormControlType.None;
            return;
        }
        if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Standard.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Standard.msg()))) {
            _controlType = FormControlType.Standard;
        } else if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Label.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Label.msg()))) {
            _controlType = FormControlType.Label;
        } else if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.SubGrid.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.SubGrid.msg()))) {
            _controlType = FormControlType.SubGrid;
        } else if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Lookup.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Lookup.msg()))) {
            _controlType = FormControlType.Lookup;
        } else if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.IFrame.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.IFrame.msg()))) {
            _controlType = FormControlType.IFrame;
        } else if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Chart.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Chart.msg()))) {
            _controlType = FormControlType.Chart;
        } else if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Grid.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Grid.msg()))) {
            _controlType = FormControlType.Grid;
        } else if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Report.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.Report.msg()))) {
            _controlType = FormControlType.Report;
        } else if (type.toString().equalsIgnoreCase(String.valueOf(FormControlType.FreeText.val()))
                || type.toString().equalsIgnoreCase(String.valueOf(FormControlType.FreeText.msg()))) {
            _controlType = FormControlType.FreeText;
        }
        else{
            _controlType = FormControlType.None;
        }
    }

    public boolean ReadOnly = false;

    private ControlParameters _parameters;

    public void setParameters(Object parameters){
        if(parameters == null) return;
        if (_controlType.val() == FormControlType.Label.val() || _controlType.val() == FormControlType.Standard.val()) {
            _parameters = JsonUtil.toBean(parameters.toString(), ExtendPropertyParameters.class);
        } else if (_controlType.val() == FormControlType.SubGrid.val()) {
            _parameters = JsonUtil.toBean(parameters.toString(), SubGridParameters.class);
        } else if (_controlType.val() == FormControlType.Lookup.val()) {
            _parameters = JsonUtil.toBean(parameters.toString(), LookUpParameters.class);
        } else if (_controlType.val() == FormControlType.IFrame.val()) {
            _parameters = JsonUtil.toBean(parameters.toString(), IFrameParameters.class);
        } else if (_controlType.val() == FormControlType.Chart.val()) {
            _parameters = JsonUtil.toBean(parameters.toString(), ChartParameters.class);
        } else if (_controlType.val() == FormControlType.Grid.val()) {
            _parameters = JsonUtil.toBean(parameters.toString(), GridParameters.class);
        } else if (_controlType.val() == FormControlType.Report.val()) {
            _parameters = JsonUtil.toBean(parameters.toString(), ReportParameters.class);
        } else if (_controlType.val() == FormControlType.FreeText.val()) {
            _parameters = JsonUtil.toBean(parameters.toString(), FreeTextparameters.class);
        }
    }

    @JSONField(name="Parameters")
    public ControlParameters getParameters(){
        return _parameters;
    }

    public String Formula;

    public Attribute AttributeMetadata;
}
